#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char** summaryRanges(int* nums, int numsSize, int* returnSize)
{
    char **p = malloc(sizeof(char*)*numsSize);
    *returnSize = 0;//先将数组元素个数置0
    int i = 0;
    while(i < numsSize)
    {
        int pos = i;//定义初始位置
        i++;
        while(i < numsSize && nums[i] == nums[i - 1] + 1)//如果i小于数组的长度并且数组i的元素等于数组i元素+1
        {
            i++;//继续往下判断
        }
        int end = i - 1;//不相等的位置
        char *s = malloc(sizeof(char)*30);//开辟一个一维数组装值
        //sprintf(s,"%d",nums[pos]);//将开始位的值装入
        
        if(pos < end)//存在两个连续的数
        {
            sprintf(s + strlen(s),"->");
            sprintf(s + strlen(s),"%d",nums[end]);
        }
        p[(*returnSize)++] = s; 
    }
    return p;
}

int main()
{
    int nums[] = {0,1,2,4,5,7};
    int numsSize = sizeof(nums)/sizeof(nums[0]);
    int returnSize = 0;
    char **s = summaryRanges(nums,numsSize,&returnSize);
    for(int i = 0;i < returnSize;i++)
        printf("%s  ",s[i]);
    printf("\n");

    free(s);
    s = NULL;
    return 0;
}
